package org.xtext.example.mydsl.ui;


import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import org.gnu.glpk.GlpkSolver;
import lpsolve.LpSolveException;

public class Controller {

        private static Runtime runtime;
        private static Process process;
        private static String path = System.getProperty("user.home") + "/util";



        /**
         * applica l'algoritmo di type inference al termine preso in input e poi
         * richiama il risolutore di vincoli .
         * @param term termine
         * @throws IOException
         * @throws InterruptedException
         */
        public static void infDlal(String term) throws IOException,
                        InterruptedException {
                runtime = Runtime.getRuntime();
                term = "\"" + term + ";;\"";

                process = runtime.exec(new String[] {
                                "bash",
                                "-c",
                                "cd " + path + "/typeDlal && ./dlalscript.sh constraint  type "
                                                + term });
        }

        
        public static String lciInterpreter(String term) throws IOException,
                        InterruptedException {
                runtime = Runtime.getRuntime();
                term = " \"" + term + "\"";
                File res = new File(path + "/lci/interpResult");
                if (res.exists())
                        res.delete();

                process = runtime.exec(new String[] { "bash", "-c",
                                "cd " + path + "/lci && ./lciscript.sh  " + term });

                InputStream inp = process.getInputStream();
                String temp = "";
                int n = 0;
                boolean done = false;
                while ((n = inp.read()) != -1) {
                        if (done)
                                temp += "" + (char) n;
                        if ((char) n == '>')
                                done = true;
                }
                return temp;
        }
       
        

        
        public static void main(String a []) throws LpSolveException{
        	HashMap<String, Double> map = getVarValue();
        	for(String e : map.keySet()){
        		System.out.println(e+" = "+map.get(e));
        	}
        	
        }
        
        public static HashMap<String,Double> getVarValue(){
                HashMap<String, Double> map = new HashMap<String, Double>();
                String userhome = System.getProperty("user.home");
                GlpkSolver solver = new GlpkSolver();
                GlpkSolver glp = solver.readCpxlp(userhome+"/util/typeDlal/constraint");
                glp.simplex();
                glp.integer();
                for(int i=1; i<=glp.getNumCols(); i++){
                        map.put(glp.getColName(i), glp.mipColVal(i));          
                }                      
                return map;
        }
       

       
        public static String getTerm(String term) throws IOException, InterruptedException{
        	String type ="";
        	infDlal(term);
        	HashMap<String, Double> map = getVarValue();
        	File file = new File(path+"/typeDlal/type");
        	FileReader filreader = new FileReader(file);
        	int c = 0;
        	while( (c = filreader.read()) != -1){
        		if(c == 167 || c == 65533){
        			filreader.read();
        			c = filreader.read();
        			String var ="";
        			int val= 0;
        			if(c == 'F'){
        				filreader.read();
        			   while(((char) (c = filreader.read())) != ')'){
        				     if(((char)c) == '+'){
        				    	 val+= map.get(var.trim());
        				    	 var="";
        				     }else var+= (char)c;
        			   }
        			   while(val>0){
        				   type+='§';
        				   val--;
        			   }
        			}else if(c == 'T'){
        				filreader.read();
         			   while(((char) (c = filreader.read())) != ')'){
      				     if(((char)c) == '+'){
      				    	 val+= map.get(var.trim());
    				    	 var="";
    				     }else var+= (char)c;
         			   }
        			   while(val>0){
        				   type+='!';
        				   val--;
        			   }
         			}else {
         				var+= (char)c;
           			   while(((char) (c = filreader.read())) != ')'){
      				     if(((char)c) == '+'){
    				    	 val+= map.get(var.trim());
    				    	 var="";
    				     }else var+= (char)c;
           			   }
        			   while(val>0){
        				   type+='§';
        				   val--;
        			   }
           			}

        		}else{
        			type+=((char)c);
        		}
        		
        	}
        	return type;
        }
        
        
        
        
        
}

